﻿using Kay.LazarToDo.Win._Controls;
using Kay.LazarToDo.Win.AppServices;
using Kay.LazarToDo.Win.Common;
using Kay.LazarToDo.Win.Common._Enum;
using System;
using System.Runtime.Remoting.Channels;
using System.Windows.Forms;

namespace Kay.LazarToDo.Win._Forms
{
    /// <summary>
    /// 待办事项列表
    /// </summary>
    public partial class LazarToDoList : Form
    {
        private readonly LazarToDoAppService _lazarToDoAppService;

        /// <summary>
        /// 初始化待办事项列表
        /// </summary>
        public LazarToDoList()
        {
            InitializeComponent();
            _lazarToDoAppService = new LazarToDoAppService();//初始化
        }

        private void LazarToDoList_Load(object sender, EventArgs e)
        {
            Application.DoEvents();
            RefreshData();
        }

        /// <summary>
        /// 刷新数据
        /// </summary>
        public void RefreshData()
        {
            GC.Collect();
            var data = _lazarToDoAppService.GetAllList();
            foreach (var toDo in data)
            {
                GlobalVariables.DbContext.Entry(toDo).Reference(t => t.Project).Load();
                var toDoDetails = new LazarToDoDetails
                {
                    ToDoContext = toDo.ToDo,
                    Project = $"{toDo.Project?.Name} | {toDo.CreationTime:yyyy/MM/dd}",
                    LazarToDo = toDo
                };
                var state = (ToDoState)toDo.State;
                switch (state)
                {
                    case ToDoState.ToDo:
                        toDoDetails.ToDo = true;
                        break;
                    case ToDoState.Doing:
                        toDoDetails.Doing = true;
                        break;
                    case ToDoState.Done:
                        toDoDetails.Done = true;
                        break;
                    case ToDoState.Remove:
                        toDoDetails.Remove = true;
                        break;
                    default:
                        throw new ArgumentOutOfRangeException();
                }
                toDoDetails.RemoveComplated += OnToDoDetailsRemoveComplated;
                toDoDetails.UpdateComplated += (sender, e) =>
                {
                    ClearProject();
                    RefreshData();
                };
                flowLayoutPanelToDoList.Controls.Add(toDoDetails);
            }
        }

        private void OnToDoDetailsRemoveComplated(object sender, EventArgs e)
        {
            var toDo = (LazarToDoDetails)sender;
            flowLayoutPanelToDoList.Controls.Remove(toDo);
            toDo.Dispose();
            GC.Collect();
        }

        private void linkLabelAdd_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            using (var frm = new AddLazarToDo())
            {
                var result = frm.ShowDialog();
                if (result == DialogResult.OK)
                {
                    ClearProject();
                    RefreshData();
                }
            }
            GC.Collect();
        }

        /// <summary>
        /// 清楚所有项目
        /// </summary>
        public void ClearProject()
        {
            flowLayoutPanelToDoList.SuspendLayout();
            foreach (Control control in flowLayoutPanelToDoList.Controls)
            {
                control.Dispose();
                GC.Collect();
            }
            flowLayoutPanelToDoList.Controls.Clear();
            GC.Collect();
            flowLayoutPanelToDoList.ResumeLayout();
        }
    }
}
